############################################################################

#FigureA_4_PanelB
#This figure plots the evolution of the average value of commodities rceived from January to November of 2017
############################################################################




### Setup ###################################################
# COLORS
fColor <- "black"
mColor <- "gray60"
lightColor <- "#97B8DE"
darkColor <- "#4669BD"


ration.value_total<-c("totalrec","totalnonrec")
ration.name_total <-c("total reconciled","total non-reconciled")


month.list <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug","Sep","Oct", "Nov")

rect_r <- data.frame(xmin=7, xmax=10, 
                     ymin=-Inf, ymax=Inf)

fontSize <- 16
theme_set(theme_gray(base_size = fontSize))


##############################################################
# total non reconciled 
##############################################################
r<-2
tmp.outcome<-"value"
tmp.ration <- ration.value_total[r]
tmp.ration1 <- ration.name_total[r]

tmp.data <- readstata13::read.dta13(paste(SurveyDataDir,tmp.ration,"_",tmp.outcome,"_meanreceipts_reconciliation_time_series.dta",sep = ""))

meltdf <- reshape2::melt(tmp.data[,c("month",paste(tmp.outcome,"_",tmp.ration,sep = ""),"yhat")],id="month")
meltdf$group <- NA
meltdf$group[meltdf$variable == paste(tmp.outcome,"_",tmp.ration,sep = "")] <- "Observed values,"
meltdf$group[meltdf$variable =="yhat"] <- "Fitted values,"

meltdf.se <- reshape2::melt(tmp.data[,c("month",paste(tmp.outcome,"_",tmp.ration,"_se",sep = ""),"yhat_se")],id="month")
meltdf.se$se <- 0
meltdf.se$se[meltdf.se$variable =="yhat_se"] = meltdf.se$value[meltdf.se$variable =="yhat_se"]
meltdf.se$value <- NULL
meltdf.se$group <- NA
meltdf.se$group[meltdf.se$variable == paste(tmp.outcome,"_",tmp.ration,"_se",sep = "")] <- "Observed values,"
meltdf.se$group[meltdf.se$variable =="yhat_se"] <- "Fitted values,"
meltdf.se$variable <- NULL


plot.dat1 <- merge(meltdf,meltdf.se, by = c("month","group"))
plot.dat1$lwr <- plot.dat1$value - 1.96*plot.dat1$se
plot.dat1$upr <- plot.dat1$value + 1.96*plot.dat1$se

##############################################################
# total reconciled 
##############################################################
seqalong<-seq_along(ration.value_total)
tmp.outcome <- "value"
r<-1
tmp.ration <- ration.value_total[r]
tmp.ration1 <- ration.name_total[r]

tmp.data <- readstata13::read.dta13(paste(SurveyDataDir,tmp.ration,"_",tmp.outcome,"_meanreceipts_reconciliation_time_series.dta",sep = ""))

meltdf <- reshape2::melt(tmp.data[,c("month",paste(tmp.outcome,"_",tmp.ration,sep = ""),"yhat")],id="month")
meltdf$group <- NA
meltdf$group[meltdf$variable == paste(tmp.outcome,"_",tmp.ration,sep = "")] <- "Observed values,"
meltdf$group[meltdf$variable =="yhat"] <- "Fitted values,"

meltdf.se <- reshape2::melt(tmp.data[,c("month",paste(tmp.outcome,"_",tmp.ration,"_se",sep = ""),"yhat_se")],id="month")
meltdf.se$se <- 0
meltdf.se$se[meltdf.se$variable =="yhat_se"] = meltdf.se$value[meltdf.se$variable =="yhat_se"]
meltdf.se$value <- NULL
meltdf.se$group <- NA
meltdf.se$group[meltdf.se$variable == paste(tmp.outcome,"_",tmp.ration,"_se",sep = "")] <- "Observed values,"
meltdf.se$group[meltdf.se$variable =="yhat_se"] <- "Fitted values,"
meltdf.se$variable <- NULL


plot.dat2 <- merge(meltdf,meltdf.se, by = c("month","group"))
plot.dat2$lwr <- plot.dat2$value - 1.96*plot.dat2$se
plot.dat2$upr <- plot.dat2$value + 1.96*plot.dat2$se

Lower<-min(plot.dat2$lwr)
Upper<-max(plot.dat2$upr)

##############################################################
# Combining two plots
##############################################################
plot.dat1$group <- paste(plot.dat1$group,"non-reconciled   ")
plot.dat2$group <- paste(plot.dat2$group,"reconciled    ")
plot.dat <- rbind(plot.dat1,plot.dat2)


ggplot(plot.dat, aes(x=month, y=value,group = group, colour= group, linetype = group)) +
  geom_line(size= 1.5)+
  geom_ribbon(aes(ymin=lwr,ymax=upr), alpha = 0.2, colour=NA) + 
  scale_colour_manual(values=c(fColor,darkColor,mColor,lightColor)) +
  scale_linetype_manual(values=c("solid","solid","dotted","dotted")) +
  xlab("Month") +ylab((paste("Average receipt value per rationcard")))+ 
  guides(colour = guide_legend(override.aes = list(shape = NA))) + #remove legend title
  guides(colour=guide_legend(ncol=2))+
  theme(axis.text=element_text(size=16),
        axis.title=element_text(size=16),legend.position= "bottom",
        legend.title=element_blank(), legend.text=element_text(size=16),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.key = element_blank(), legend.spacing.x = unit(0.1, 'cm'),legend.key.width = unit(1.5, "cm")) +
  scale_y_continuous(limit=c(0  , 550  ),breaks = seq(0,550,100)) +
  scale_x_continuous(limit=c(1,11), breaks = seq(1,11,1), labels = month.list) +
  geom_rect(data=rect_r, aes(xmin=xmin, xmax=xmax, 
                             ymin=ymin, ymax=ymax), 
            color="gray100", alpha=0.05, inherit.aes = F,
            linetype = 0)


ggsave(file=paste(OutputDir, "FigureA_4_PanelB.pdf",
                  sep=""), width=12, height=7)

